package com.bayair.doctor.ui.main.right;

import com.bayair.doctor.data.bean.SetResp;
import com.bayair.doctor.mvp.BasePresenter;
import com.bayair.doctor.mvp.annotation.InjectModel;
import com.bayair.doctor.net.RxFunction;
import com.bayair.doctor.net.RxSchedulers;
import com.bayair.doctor.biz.helper.SpDoctor;

public class RightPresenter extends BasePresenter<RightContract.View> implements RightContract.Presenter {

    @InjectModel
    RightModel mModel;

    @Override
    public void setReceive(boolean isReceive) {
        mModel.setReceive(isReceive)
                .map(new RxFunction<>())
                .compose(RxSchedulers.io_main())
                .subscribe(new ObserverOnRecycler<SetResp>() {

                    @Override
                    public void onNext(SetResp setResp) {
                        //成功，改变本地记录
                        SpDoctor.setReceive(isReceive);
                        toast("已完成保存");
                        getView().completeReceive();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        //处理错误
                        getView().failSaveIsReceiveMsg();
                        getView().completeReceive();
                    }
                });
    }

    @Override
    public void setInquiry(boolean isInquiry) {
        mModel.setInquiry(isInquiry)
                .map(new RxFunction<>())
                .compose(RxSchedulers.io_main())
                .subscribe(new ObserverOnRecycler<SetResp>() {

                    @Override
                    public void onNext(SetResp setResp) {
                        //成功，改变本地记录
                        SpDoctor.setInquiry(isInquiry);
                        toast("已完成保存");
                        getView().completeInquiry();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        //处理错误
                        getView().failSaveIsInquiry();
                        getView().completeInquiry();
                    }
                });
    }

    @Override
    public void logout() {
        mModel.logout()
                .map(new RxFunction<>())
                .compose(RxSchedulers.io_main())
                .subscribe(new ObserverOnRecycler<SetResp>() {
                    @Override
                    public void onNext(SetResp setResp) {
                        getView().logoutSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        //super.onError(e);
                        getView().logoutFailed();
                    }
                });
    }
}
